let _util = require('util/util.js');

let locationOfFileService = {
    // 加载档号与位置关系信息
    loadTableInfo: function (params) {
        return new Promise((resolve, reject) => {
            _util.request({
                url: 'http://localhost:3000/locationAndFileNo/',
                // url: 'http://111.231.233.160:8080/location/getLocationList',
                data: params.data,
                success: function (res) {
                    let newObj = $.extend({}, { data: res }, params);
                    resolve(newObj);
                },
                error: function (err) {
                    console.log(err);
                }
            })
        })
    },
    //获取单条档号位置信息，以作修改
    getFileLocationInfo: function (params) {
        return new Promise((resolve, reject) => {
            _util.request({
                url: 'http://localhost:3000/locationAndFileNo/',
                data: params.data,
                success: function (res) {
                    resolve(res);
                },
                error: function (errMsg) {
                    reject(res);
                }
            })
        });
    },
    //保存档号和位置信息的编辑修改
    saveFileLocationInfo: function (params) {
        return new Promise((resolve, reject) => {
            _util.request({
                url: 'http://localhost:3000/uploads/',
                data: params.data,
                xhr: function () {
                    let xhr = new XMLHttpRequest();

                    xhr.addEventListener('progress', function (evt) {
                        if (evt.lengthComputable) {
                            let loaded = evt.loaded,
                                total = evt.total,
                                percentage = (loaded / total) * 100;

                            if (percentage === 100) {
                                $('.save-success').css('display', 'block');
                            }

                            $('.progress .progress-bar').css('width', `${percentage}%`);
                            $('.progress .progress-bar').text(`${percentage}%`)
                        }
                    })

                    return xhr;
                },
                success: function (res) {
                    resolve(res);
                },
                error: function (errMsg) {
                    reject(res);
                }
            })
        })
    }
}

module.exports = locationOfFileService;